package gamecontroler;

import viewer.StandardGameView;
import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

// this class use to store the canvas
// SurfaceView in this case just for holder, no drawing method is invoked!
// browsing between pages or game

public class GameLoop extends SurfaceView implements Runnable {

	// canvas control
	private Canvas canvas;
	private SurfaceHolder holder;

	// control the game state and options
	private GameState gameState = GameState.getGameState();
	private GameControler gameControler = GameControler.getGameControler();

	// view game screen
	private StandardGameView gameView;

	// control the browsing
	// private BrowserControler browse= new BrowserControler();

	// list of browsing pages here
	// private HomeView homeView;

	// ****thread of update and drawing on screen
	private Thread controlViewScreen = null;

	// track back to know X,Y
	// private MainActivity mainActivity;

	// check if gameloop can continue
	private boolean isRunning = false;

	public int FPS = 30;
	private long ticksPs = 1000 / FPS;
	private long startTime;
	private long sleepTime;

	// constructor here!
	public GameLoop(Context context) {
		super(context);
		// this.mainActivity = (MainActivity) context;
		holder = getHolder();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub

		while (isRunning == true) {
			// start the time
			startTime = System.currentTimeMillis();

			// check if holder is ok to take action
			if (!holder.getSurface().isValid())
				continue;

			// lock canvas to start paint
			canvas = holder.lockCanvas();

			// handle the touch action
			// #####################################################33
			gameControler.handleTouchEvent(gameState);

			// select approriate screen to view
			// BrowserControler.seleteScreenToView();
			gameView.drawOut(canvas);

			// *********************************************************

			// unlock canvas and show to the world
			holder.unlockCanvasAndPost(canvas);

			// update the time
			updateTime();
		}

	}

	private void updateTime() {
		sleepTime = ticksPs - (System.currentTimeMillis() - startTime);
		try {
			if (sleepTime > 0)
				Thread.sleep(sleepTime);
			else
				Thread.sleep(0);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void onPause() {
		isRunning = false;
		while (true) {
			try {
				controlViewScreen.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			break;
		}
		controlViewScreen = null;
	}

	public void onResume() {
		isRunning = true;
		controlViewScreen = new Thread(this);
		controlViewScreen.start();
	}

	private void loadAllScreen() {
		if ((gameView == null) && (gameState != null))
			this.gameView = new StandardGameView(gameState, getResources(),
					false);
	}

	public void load() {
		loadAllScreen();
	}
}
